package com.frxuexi.park2301

import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Color
import android.os.Build
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.navigation.findNavController
import androidx.navigation.ui.NavigationUI
import com.alibaba.android.arouter.facade.annotation.Route
import com.frxuexi.lib_base.mvvm.BaseViewActivity
import com.frxuexi.park2301.databinding.ActivityMainBinding
import dagger.hilt.android.AndroidEntryPoint


@Route(path = "/app/MainActivity")
@AndroidEntryPoint
class MainActivity : BaseViewActivity<ActivityMainBinding>() {
    private val LOCATION_PERMISSION_REQUEST_CODE = 1001
    override fun initData() {

    }

    override fun initView() {
        val navController = findNavController(R.id.fragment_main)
        NavigationUI.setupWithNavController(binding.nav,navController)

        //显示气泡
        val badge = binding.nav.getOrCreateBadge(R.id.messageFragment)
        badge.number=10
        badge.backgroundColor=Color.RED
        badge.badgeTextColor=Color.WHITE

        //点击气泡消失
//        binding.nav.setOnNavigationItemSelectedListener {
//            if(it.itemId==R.id.messageFragment){
//                binding.nav.removeBadge(R.id.messageFragment)
//            }
//            return@setOnNavigationItemSelectedListener false
//        }

//        if (checkLocationPermission()) {
//            startLocationService();
//        }
    }

    private fun checkLocationPermission(): Boolean {
        if (ContextCompat.checkSelfPermission(this, "android.permission.ACCESS_FINE_LOCATION")
            != PackageManager.PERMISSION_GRANTED
        ) {
            ActivityCompat.requestPermissions(
                this, arrayOf<String>("android.permission.ACCESS_FINE_LOCATION"),
                LOCATION_PERMISSION_REQUEST_CODE
            )
            return false
        }
        return true
    }

    private fun startLocationService() {
        val serviceIntent = Intent(this, LocationTrackingService::class.java)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            startForegroundService(serviceIntent)
        } else {
            startService(serviceIntent)
        }
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<String?>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == LOCATION_PERMISSION_REQUEST_CODE) {
            if (grantResults.size > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                startLocationService()
            }
        }
    }
}